
#define __notcgi
#include "cgi.h"

#include "json.h"

using namespace json;
using namespace cppcgi;

#include <cstdlib>
#include <direct.h>
#include <string>
#include <cstring>
#include <io.h>
#include <cstdio>
#include <iostream>

using namespace std;

#ifndef _MAX_PATH
#define _MAX_PATH 256
#endif // _MAX_PATH

#define COMPACT_CGI_LOG_VINFO LogMessage("I "RELEASE_VERSION, \
            id_string).Stream() << __FUNCTION__ << ":" << __LINE__ << "] "

#define COMPACT_CGI_LOG_VERROR LogMessage("E "RELEASE_VERSION, \
            id_string).Stream() << __FUNCTION__ << ":" << __LINE__ << "] "

const string id_string = "12";

typedef struct rconnect
{
    //business id
    int id;
    //business identitification
    string username;
    int identity;
    //business error
    int errnum;
    string errmsg;
} ConnSession;

ConnSession user_info = {1,"aa",0,0,""};

int GeneralCode(const string configFile)
{
    string s;
    string t;
    OrderedConfigSet c(configFile);
    if (c.Empty())
        return -1;
    KvVector param = c["_param"];
    KvVector main = c["_main"];

    //KvVector filter = c["_filterfuzzy"];
    KvVector filter = param;
    KvVector filterunion = c["_filterunion"];

    KvVector sort = c["_sort"];

    string db = main["db"];
    string entity = main["entity"];
    string mapper = main["mapper"];
    string dao = entity + "Dao";
    string daoimpl = entity + "DaoImpl";
    string service = entity + "Service";
    string serviceimpl = entity + "ServiceImpl";
    string obj = main["obj"];
    string jsp = main["jsp"];
    string indexparam = main["indexparam"];

    if (indexparam == "")
        indexparam = "id";

    //entity.java
    File* f = File::Open("com/dunchong/entity/" + entity + ".java", "w");
    f->AddLine("package com.dunchong.entity;");
    f->AddLine("");
    f->AddLine("import javax.validation.constraints.*;");
    f->AddLine("");
    f->AddLine("public class " + entity + " implements java.io.Serializable {");

    time_t rawtime;
    time(&rawtime);

    f->AddLine("    private static final long serialVersionUID = -" + CIntStr(rawtime).ToString() + "036947282L;");
    int ps = param.Size();
    for (int i = 0; i < ps; i++)
    {
        f->AddLine("    private " + param.GetValue(i) + " " + param.GetKey(i) + ";");
    }

    f->AddLine("");
    f->AddLine("    public " + entity + "() {");
    f->AddLine("");
    f->AddLine("    }");

    for (int i = 0; i < ps; i++)
    {
        t = param.GetValue(i);
        s = param.GetKey(i);

        f->AddLine("");
        f->AddLine("    public " + t + " get" + ToProperCase(s) + "() {");
        f->AddLine("        return " + s + ";");
        f->AddLine("    }");

        f->AddLine("");
        f->AddLine("    public void set" + ToProperCase(s) + "(" + t + " " + s + ") {");
        f->AddLine("        this." + s + " = " + s + ";");
        f->AddLine("    }");
    }
    f->AddLine("");
/*
    f->AddLine("    public Map<String, Object> toEntryMap() {");
	f->AddLine("	    Map<String, Object> objMap = new HashMap<String, Object>();");

	for (int i = 0; i < ps; i++)
    {
        s = param.GetKey(i);
        f->AddLine("        objMap.put(\"" + ToLowCase(entity) + "_" + s + "\", " + s + ");");
    }
    f->AddLine("        return objMap;");
    f->AddLine("    }");
    f->AddLine("");
*/
    f->AddLine("    @Override");
    f->AddLine("    public String toString() {");
    f->AddLine("        return \"" + entity + " [\"");
    for (int i = 0; i < ps; i++)
    {
        s = param.GetKey(i);
        if (i != ps - 1)
            f->AddLine("            + \"" + s + "=\" + " + s + " + \", \"");
        else
            f->AddLine("            + \"" + s + "=\" + " + s + " + \"]\";");
    }
    f->AddLine("    }");
    f->AddLine("}");
    f->Save();

    //mapper.xml
    f = File::Open("com/dunchong/entity/mapper/" + mapper + ".xml", "w");
    f->AddLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    f->AddLine("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" ");
    f->AddLine("    \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
    f->AddLine("<mapper namespace=\"" + ToLowCase(entity) + "\">");
    f->AddLine("    <sql id=\"selectId\">");

    for (int i = 0; i < ps; i++)
    {
        if (i != ps - 1)
            f->AddLine("        " + param.GetKey(i) + ",");
        else
            f->AddLine("        " + param.GetKey(i));
    }
    f->AddLine("    </sql>");
    f->AddLine("");
    f->AddLine("    <select id=\"query\" resultType=\"" + entity + "\" parameterType=\"java.util.HashMap\">");
    f->AddLine("        select ");
    f->AddLine("        <include refid=\"selectId\" />");
    f->AddLine("        from " + db + " ");
    f->AddLine("        <where>");

    for (int i = 0; i < filter.Size(); i++)
    {
        s = filter.GetKey(i);
        t = filter.GetValue(i);
        if (t == "Byte")
            f->AddLine("            <if test=\"t." + s + " != null\">");
        else
            f->AddLine("            <if test=\"t." + s + " != null and t." + s + " != ''\">");

        if (i == 0)
        {
            if (t != "String")
                f->AddLine("                " + s + "=${t." + s + "}");
            else
                f->AddLine("                " + s + " like '%${t." + s + "}%'");
        }
        else
        {
            if (t != "String")
                f->AddLine("                AND " + s + "=${t." + s + "}");
            else
                f->AddLine("                AND " + s + " like '%${t." + s + "}%'");
        }


        f->AddLine("            </if>");
    }

//------------------------------------------------------------------
    for (int i = 0; i < filter.Size(); i++)
    {
        s = filter.GetKey(i);
        t = filter.GetValue(i);
        if (t == "Byte")
            Print("            <if test=\"t." + ToLowCase(entity) + "_" + s + " != null\">");
        else
            Print("            <if test=\"t." + ToLowCase(entity) + "_" + s + " != null and t." + ToLowCase(entity) + "_" + s + " != ''\">");

        if (i == 0)
        {
            if (t != "String")
                Print("                a." + s + "=${t." + ToLowCase(entity) + "_" + s + "}");
            else
                Print("                a." + s + " like '%${t." + ToLowCase(entity) + "_" + s + "}%'");
        }
        else
        {
            if (t != "String")
                Print("                AND a." + s + "=${t." + ToLowCase(entity) + "_" + s + "}");
            else
                Print("                AND a." + s + " like '%${t." + ToLowCase(entity) + "_" + s + "}%'");
        }


        Print("            </if>");
    }
    Print("");
//------------------------------------------------------------------

    f->AddLine("        </where>");
    f->AddLine("    </select>");
    f->AddLine("");


    f->AddLine("    <select id=\"queryExact\" resultType=\"" + entity + "\" parameterType=\"java.util.HashMap\">");
    f->AddLine("        select ");
    f->AddLine("        <include refid=\"selectId\" />");
    f->AddLine("        from " + db + " ");
    f->AddLine("        <where>");

    for (int i = 0; i < filter.Size(); i++)
    {
        s = filter.GetKey(i);
        t = filter.GetValue(i);
        if (t == "Byte")
            f->AddLine("            <if test=\"t." + s + " != null\">");
        else
            f->AddLine("            <if test=\"t." + s + " != null and t." + s + " != ''\">");

        if (i == 0)
        {
            if (t == "String")
                f->AddLine("                " + s + "='${t." + s + "}'");
            else
                f->AddLine("                " + s + "=${t." + s + "}");
        }
        else
        {
            if (t == "String")
                f->AddLine("                AND " + s + "='${t." + s + "}'");
            else
                f->AddLine("                AND " + s + "=${t." + s + "}");
        }


        f->AddLine("            </if>");
    }

    f->AddLine("        </where>");
    f->AddLine("    </select>");
    f->AddLine("");

    if (!filterunion.Empty())
    {
        string wijianselect = "";
        string wijianleftjoin = "";
        string wijianassociation = "";

        string alias = main["alias"];
        //提前
        for (int i = 0; i < filterunion.Size(); i++)
        {
            string wjid =  filterunion.GetKey(i);
            string wjset = filterunion.GetValue(i);
            KvVector subMain = c[wjset];
            KvVector subParam = c[wjset + "-param"];
            wijianassociation += "        <association property=\"" + wjset + "\" javaType=\"" + subMain["javatype"] + "\">\n";
            for (int j = 0; j < subParam.Size(); j++)
            {
                s = subParam.GetKey(j);
                t = subMain["alias"];
                wijianselect += "            " + t + "." + s + " as " + t + "_" + s + ",\n";

                wijianassociation += "            <result property=\"" + s + "\" column=\"" + t + "_" + s + "\" />\n";
            }
            wijianassociation += "        </association>\n";
            wijianleftjoin += "        left OUTER join " + subMain["db"] + " " + t + " ON " + alias + "." + wjid + " = " + t + "." + subMain["indexparam"] + "\n";
        }


        f->AddLine("    <select id=\"queryUnion\" resultMap=\"allData\" parameterType=\"java.util.HashMap\">");
        f->AddLine("        select ");

        for (int i = 0; i < ps; i++)
        {
            s = param.GetKey(i);
            f->AddLine("            " + alias + "." + s + " as " + s + ",");
        }
        f->AddLine(wijianselect.substr(0, wijianselect.length() - 2));

        f->AddLine("        from (");
        f->AddLine("            select * from " + db);
        f->AddLine("            <where>");

        for (int i = 0; i < filter.Size(); i++)
        {
            s = filter.GetKey(i);
            t = filter.GetValue(i);
            if (t == "Byte")
                f->AddLine("            <if test=\"t." + s + " != null\">");
            else
                f->AddLine("            <if test=\"t." + s + " != null and t." + s + " != ''\">");

            if (i == 0)
            {
                if (t != "String")
                    f->AddLine("                " + s + "=${t." + s + "}");
                else
                    f->AddLine("                " + s + " like '%${t." + s + "}%'");
            }
            else
            {
                if (t != "String")
                    f->AddLine("                AND " + s + "=${t." + s + "}");
                else
                    f->AddLine("                AND " + s + " like '%${t." + s + "}%'");
            }


            f->AddLine("            </if>");
        }
        f->AddLine("            </where>");
        f->AddLine("        ) as " + alias);

        f->AddLine(wijianleftjoin.substr(0, wijianleftjoin.length() - 1));
        f->AddLine("    </select>");

        f->AddLine("");

        f->AddLine("    <resultMap id=\"allData\" type=\"java.util.HashMap\">");

        for (int i = 0; i < ps; i++)
        {
            s = param.GetKey(i);
            f->AddLine("        <result property=\"" + s + "\" column=\"" + s + "\" />");
        }
        f->AddLine(wijianassociation.substr(0, wijianassociation.length() - 1));
        f->AddLine("    </resultMap>");
        f->AddLine("");
    }


    f->AddLine("    <insert id=\"add\" parameterType=\"" + entity + "\">");
    f->AddLine("        insert into " + db + " ( ");
    for (int i = 0; i < ps; i++)
    {
        s = param.GetKey(i);
        if (s == "id")
            continue;
        else
        {
            if (i != ps - 1)
                f->AddLine("            " + param.GetKey(i) + ",");
            else
                f->AddLine("            " + param.GetKey(i));
        }
    }
    f->AddLine("        ) values ( ");
    for (int i = 0; i < ps; i++)
    {
        s = param.GetKey(i);
        if (s == "id")
            continue;
        else
        {
            if (i != ps - 1)
                f->AddLine("            #{" + param.GetKey(i) + "},");
            else
                f->AddLine("            #{" + param.GetKey(i) + "}");
        }
    }
    f->AddLine("        ) ");
    f->AddLine("    </insert> ");
    f->AddLine("");
    f->AddLine("    <delete id=\"deleteById\" parameterType=\"String\">");
    f->AddLine("        delete from " + db + " where");
    f->AddLine("        " + indexparam + "=#{" + indexparam + "}");
    f->AddLine("    </delete>");
    f->AddLine("");
    f->AddLine("    <delete id=\"deleteByIds\" parameterType=\"String\">");
    f->AddLine("        delete from " + db + " where " + indexparam +" in");
    f->AddLine("        <foreach item=\"idItem\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">");
    f->AddLine("            #{idItem}");
    f->AddLine("        </foreach>");
    f->AddLine("    </delete>");
    f->AddLine("");
    f->AddLine("    <select id=\"getById\" parameterType=\"String\" resultType=\"" + entity + "\">");
    f->AddLine("        select ");
    f->AddLine("        <include refid=\"selectId\" />");
    f->AddLine("        from " + db + " where " + indexparam + "=#{" + indexparam + "}");
    f->AddLine("    </select>");
    f->AddLine("");
    f->AddLine("    <update id=\"update\" parameterType=\"" + entity + "\">");
    f->AddLine("        update " + db + " ");
    f->AddLine("        <set>");

    for (int i = 0; i < ps; i++)
    {
        s = param.GetKey(i);
        if (s == indexparam)
            continue;
        else
        {
            f->AddLine("            <if test=\"" + s + " != null\">");
            f->AddLine("                " + s + "=#{" + s + "},");
            f->AddLine("            </if>");
        }
    }

    f->AddLine("        </set>");
    f->AddLine("        where " + indexparam + "=#{" + indexparam + "}");
    f->AddLine("    </update>");
    f->AddLine("</mapper>");
    f->Save();

    //dao
    f = File::Open("com/dunchong/dao/" + dao + ".java", "w");
    f->AddLine("package com.dunchong.dao;");
    f->AddLine("");
    if (!filterunion.Empty())
    {
        f->AddLine("import java.util.List;");
        f->AddLine("import java.util.Map;");
    }
    f->AddLine("import com.dunchong.base.BaseDao;");
    if (!filterunion.Empty())
        f->AddLine("import com.dunchong.entity.PageView;");
    f->AddLine("import com.dunchong.entity." + entity + ";");
    f->AddLine("");
    f->AddLine("public interface " + dao + " extends BaseDao<" + entity + "> {");
    if (!filterunion.Empty())
    {
        f->AddLine("    public List<Map<String, Object>> queryUnion(PageView pageView, " + entity + " " + obj + ");");
    }
    f->AddLine("}");
    f->Save();

    //daoimpl
    f = File::Open("com/dunchong/dao/impl/" + daoimpl + ".java", "w");
    f->AddLine("package com.dunchong.dao.impl;");
    f->AddLine("");
    if (!filterunion.Empty())
    {
        f->AddLine("import java.util.List;");
        f->AddLine("import java.util.Map;");
        f->AddLine("import java.util.HashMap;");
    }

    f->AddLine("import org.springframework.stereotype.Repository;");
    f->AddLine("");
    f->AddLine("import com.dunchong.base.impl.BaseDaoImpl;");
    f->AddLine("import com.dunchong.dao."+ dao + ";");
    if (!filterunion.Empty())
        f->AddLine("import com.dunchong.entity.PageView;");
    f->AddLine("import com.dunchong.entity." + entity + ";");
    f->AddLine("");
    f->AddLine("@Repository(\"" + obj + "Dao\")");
    f->AddLine("public class " + daoimpl + " extends BaseDaoImpl<" + entity + "> implements " + dao + " {");
    if (!filterunion.Empty())
    {
        f->AddLine("    public List<Map<String, Object>> queryUnion(PageView pageView, " + entity + " " + obj + ") {");
        f->AddLine("        Map<Object, Object> map = new HashMap<Object, Object>();");
        f->AddLine("        map.put(\"paging\", pageView);");
        f->AddLine("        map.put(\"t\", " + obj + ");");
        f->AddLine("        return getSqlSession().selectList(this.getClassName() + \".queryUnion\", map);");
        f->AddLine("    }");
    }
    f->AddLine("}");
    f->Save();

    //service
    f = File::Open("com/dunchong/service/" + service + ".java", "w");
    f->AddLine("package com.dunchong.service;");
    f->AddLine("");
    if (!filterunion.Empty())
        f->AddLine("import com.dunchong.entity.PageView;");
    f->AddLine("import com.dunchong.entity." + entity + ";");
    f->AddLine("");
    f->AddLine("public interface " + service + " extends BaseService<" + entity + "> {");

    if (!filterunion.Empty())
    {
        f->AddLine("    public PageView queryUnion(PageView pageView, " + entity + " " + obj + ");");
    }
    f->AddLine("}");
    f->Save();

    //serviceimpl
    f = File::Open("com/dunchong/service/impl/" + serviceimpl + ".java", "w");
    f->AddLine("package com.dunchong.service.impl;");
    f->AddLine("");
    f->AddLine("import java.util.List;");
    if (!filterunion.Empty())
    {
        f->AddLine("import java.util.Map;");
        f->AddLine("import java.lang.reflect.Field;");
    }
    f->AddLine("import org.springframework.stereotype.Service;");
    f->AddLine("import org.springframework.transaction.annotation.Transactional;");
    f->AddLine("");
    f->AddLine("import com.dunchong.base.impl.BaseServiceImpl;");
    f->AddLine("import com.dunchong.entity." + entity + ";");
    if (!filterunion.Empty())
    {
        f->AddLine("import com.dunchong.entity.PageView;");
        f->AddLine("import com.dunchong.dao." + entity + "Dao;");
    }

    f->AddLine("import com.dunchong.service." + service + ";");
    f->AddLine("");
    f->AddLine("@Transactional");
    f->AddLine("@Service(\"" + obj + "Service\")");
    f->AddLine("public class " + serviceimpl + " extends BaseServiceImpl<" + entity + "> implements " + service + " {");

    if (!filterunion.Empty())
    {
        f->AddLine("    public PageView queryUnion(PageView pageView, " + entity + " " + obj + ") {");
        f->AddLine("        Field[] field = " + obj + ".getClass().getDeclaredFields();");
        if (sort.Empty())
        {
            f->AddLine("        String[] supportSortKeys = new String[field.length];");
            f->AddLine("        for (int i = 0; i < field.length; i++)");
            f->AddLine("            supportSortKeys[i] = field[i].getName();");
            f->AddLine("        ");
        }
        else
        {
            f->AddLine("        String[] supportSortKeys = new String[field.length + " + CIntStr(sort.Size()).ToString() + "];");
            f->AddLine("        for (int i = 0; i < field.length; i++)");
            f->AddLine("            supportSortKeys[i] = field[i].getName();");
            f->AddLine("        ");
            f->AddLine("		int cout = field.length;");

            for (int i = 0; i < sort.Size(); i++)
            {
                f->AddLine("		supportSortKeys[cout++] = \"" + sort.GetKey(i) + "\";");
            }
        }
        f->AddLine("        pageView.setSupportSortKeys(supportSortKeys);");
        f->AddLine("        List<Map<String, Object>> list = ((" + entity + "Dao) dao).queryUnion(pageView, " + obj + ");");
        f->AddLine("        pageView.setRecords(list);");
        f->AddLine("        return pageView;");
        f->AddLine("    }");
    }

    f->AddLine("	public " + entity + " getByCombinedIndex(" + entity + " " + obj + ") {");
    f->AddLine("        " + entity + " tmp = new " + entity + "();");
    f->AddLine("        tmp.setAccount_id(" + obj + ".getAccount_id());");
    f->AddLine("        tmp.setKey_name(" + obj + ".getKey_name());");
    f->AddLine("        List<" + entity + "> list = dao.query(tmp);");
    f->AddLine("        if (list.size() > 0)");
    f->AddLine("            return list.get(0);");
    f->AddLine("        else");
    f->AddLine("            return null;");
    f->AddLine("    }");
    f->AddLine("");
    f->AddLine("    public Boolean isExist(" + entity + " " + obj + ") {");
    f->AddLine("        if (getByCombinedIndex(" + obj + ") != null)");
    f->AddLine("            return true;");
    f->AddLine("        else");
    f->AddLine("            return false;");
    f->AddLine("    }");

    f->AddLine("}");
    f->Save();

    //controller
    f = File::Open("com/dunchong/controller/" + entity + "Controller.java", "w");
    f->AddLine("package com.dunchong.controller;");
    f->AddLine(" ");
    f->AddLine("import java.util.Map;");
    f->AddLine(" ");
    f->AddLine("import javax.annotation.PostConstruct;");
    f->AddLine("import javax.servlet.http.HttpServletRequest;");
    f->AddLine(" ");
    f->AddLine("import org.springframework.beans.factory.annotation.Autowired;");
    f->AddLine("import org.springframework.stereotype.Controller;");
    f->AddLine("import org.springframework.ui.Model;");
    f->AddLine("import org.springframework.web.bind.annotation.RequestMapping;");
    f->AddLine("import org.springframework.web.bind.annotation.ResponseBody;");
    f->AddLine(" ");
    f->AddLine("import com.dunchong.entity." + entity + ";");
    f->AddLine("import com.dunchong.service." + entity + "Service;");
    f->AddLine("import com.dunchong.util.g;");
    f->AddLine("import com.dunchong.entity.PageView;");
    f->AddLine("import com.dunchong.exception.DCException;");
    f->AddLine("import com.dunchong.general.QuerySet;");
    f->AddLine("import com.dunchong.general.ResultSet;");
    f->AddLine("import com.dunchong.general.UserAccount;");
    f->AddLine("import com.dunchong.general.WebMessage;");
    f->AddLine(" ");
    f->AddLine("@Controller");
    f->AddLine("@RequestMapping(\"/" + jsp + "\")");
    f->AddLine("public class " + entity + "Controller {");
    f->AddLine(" ");
    f->AddLine("    @Autowired UserAccount account;");
    f->AddLine("     ");
    f->AddLine("    @Autowired QuerySet querySet;");
    f->AddLine("    ");
    f->AddLine("    @Autowired ResultSet resultSet;");
    f->AddLine("    ");
    f->AddLine("    @Autowired WebMessage webMessage;");
    f->AddLine(" ");
    f->AddLine("    @Autowired " + entity + "Service " + obj + "Service;");
    f->AddLine(" ");
    f->AddLine("    @PostConstruct");
    f->AddLine("    public void init() {");
    f->AddLine("        resultSet.setSection(webMessage.getMessage(\"" + ToLowCase(entity) + ".title\"));");
    f->AddLine("    }");
    f->AddLine(" ");
    f->AddLine("    @RequestMapping(\"\")");
    f->AddLine("    public String show(Model model) {");
    f->AddLine("        return \"" + jsp + "\";");
    f->AddLine("    }");
    f->AddLine(" ");
    f->AddLine("    @RequestMapping(params=\"_op=1\")");
    f->AddLine("    @ResponseBody");
    f->AddLine("    public Map<String, Object> add(" + entity + " " + obj + ") {");
    f->AddLine("        resultSet.clear();");
    f->AddLine("        " + obj + ".setAccount_id(account.getAccount_id());");
    f->AddLine("        try {");
    f->AddLine("            g.getLogger().info(\"add \" + " + obj + ".toString());");
    f->AddLine("            try {");
    f->AddLine("                " + obj + "Service.add(" + obj + ");");
    f->AddLine("            } catch (DCException e) {");
    f->AddLine("                resultSet.setError(e.getMessage());");
    f->AddLine("                g.getLogger().error(e);");
    f->AddLine("            }");
    f->AddLine("        } catch (Exception e) {");
    f->AddLine("            g.getLogger().error(e);");
    f->AddLine("            resultSet.setError(\"error.connect.db\");");
    f->AddLine("        }");
    f->AddLine("        return resultSet.render();");
    f->AddLine("    }");
    f->AddLine(" ");
    f->AddLine("    @RequestMapping(params=\"_op=2\")");
    f->AddLine("    @ResponseBody");
    f->AddLine("    public Map<String, Object> delete(String[] id, HttpServletRequest request) {");
    f->AddLine("        resultSet.clear();");
    f->AddLine("        if (id != null && id.length > 0)");
    f->AddLine("        {");
    f->AddLine("            for (int i = 0; i < id.length; i++)");
    f->AddLine("            {");
    f->AddLine("                " + entity + " " + obj + " = null;");
    f->AddLine("                try {");
    f->AddLine("                    " + obj + " = " + obj + "Service.getById(id[i]);");
    f->AddLine("                } catch (Exception e) {");
    f->AddLine("                    g.getLogger().error(e);");
    f->AddLine("                    resultSet.setError(\"error.connect.db\");");
    f->AddLine("                    return resultSet.render();");
    f->AddLine("                }");
    f->AddLine("                if (" + obj + " == null || !" + obj + ".getAccount_id().equals(account.getAccount_id()))");
    f->AddLine("                {");
    f->AddLine("                    String[] strings = {g.getRemoteIp(request)};");
    f->AddLine("                    g.getLogger().error(webMessage.getMessage(\"illegal.delete\", strings));");
    f->AddLine("                    return resultSet.render();");
    f->AddLine("                }");
    f->AddLine("            }");
    f->AddLine("");
    f->AddLine("            g.getLogger().info(\"delete " + entity + " \" + " + indexparam + ".toString());");
    f->AddLine(" ");
    f->AddLine("            try {");
    f->AddLine("                " + obj + "Service.delete(" + indexparam + ");");
    f->AddLine("            } catch (DCException e) {");
    f->AddLine("                resultSet.setError(e.getMessage());");
    f->AddLine("                g.getLogger().error(e);");
    f->AddLine("            } catch (Exception e) {");
    f->AddLine("                g.getLogger().error(e);");
    f->AddLine("                resultSet.setError(\"error.connect.db\");");
    f->AddLine("            }");
    f->AddLine("        }");
    f->AddLine("        return resultSet.render();");
    f->AddLine("    }");
    f->AddLine(" ");
    f->AddLine("    @RequestMapping(params=\"_op=3\")");
    f->AddLine("    @ResponseBody");
    f->AddLine("    public Map<String, Object> modify(" + entity + " " + obj + ", HttpServletRequest request) {");
    f->AddLine("        resultSet.clear();");
    f->AddLine("        try {");
    f->AddLine("            //获取原有配置");
    f->AddLine("            " + entity + " oldAccessControl = " + obj + "Service.getById(" + obj + ".getId().toString());");
    f->AddLine(" ");
    f->AddLine("            if (oldAccessControl == null || !oldAccessControl.getAccount_id().equals(account.getAccount_id()))");
    f->AddLine("            {");
    f->AddLine("                String[] strings = {g.getRemoteIp(request)};");
    f->AddLine("                g.getLogger().error(webMessage.getMessage(\"illegal.modify\", strings));");
    f->AddLine("                return resultSet.render();");
    f->AddLine("            }");
    f->AddLine("            g.getLogger().info(\"modify \" + " + obj + ".toString());");
    f->AddLine("            //执行修改");
    f->AddLine("            try {");
    f->AddLine("                " + obj + "Service.modify(" + obj + ");");
    f->AddLine("            } catch (DCException e) {");
    f->AddLine("                resultSet.setError(e.getMessage());");
    f->AddLine("                g.getLogger().error(e);");
    f->AddLine("            }");
    f->AddLine("        } catch (Exception e) {");
    f->AddLine("            resultSet.setError(\"error.connect.db\");");
    f->AddLine("            g.getLogger().error(e);");
    f->AddLine("        }");
    f->AddLine("        return resultSet.render();");
    f->AddLine("    }");
    f->AddLine(" ");
    f->AddLine("    @RequestMapping(params=\"_op=4\")");
    f->AddLine("    @ResponseBody");
    f->AddLine("    public Map<String, Object> query(" + entity + " " + obj + ", HttpServletRequest request) {");
    f->AddLine("        resultSet.clear();");
    f->AddLine("        querySet.init(request);");
    f->AddLine("        PageView pageView = new PageView();");
    f->AddLine("        querySet.popPageView(pageView);");
    f->AddLine("        " + obj + ".setAccount_id(account.getAccount_id());");
    f->AddLine("        g.getLogger().info(\"query \" + " + obj + ".toString());");
    f->AddLine("        try {");
    if (!filterunion.Empty())
        f->AddLine("            pageView = " + obj + "Service.queryUnion(pageView, " + obj + ");");
    else
        f->AddLine("            pageView = " + obj + "Service.query(pageView, " + obj + ");");
    f->AddLine("            resultSet.setFilternum(pageView.getRowCount());");
    f->AddLine("            resultSet.setRows(pageView.getRecords());");
    f->AddLine("        } catch (Exception e) {");
    f->AddLine("            g.getLogger().error(e);");
    f->AddLine("            resultSet.setError(\"error.connect.db\");");
    f->AddLine("        }");
    f->AddLine("        return resultSet.render();");
    f->AddLine("    }");
    f->AddLine("}");
    f->Save();

    f = File::Open("com/mybatis-alias.xml", "a");
    f->AddLine("<typeAlias type=\"com.dunchong.entity." + entity + "\" alias=\"" + entity + "\" />");
    f->Save();

    f = File::Open("com/mybatis-mapper.xml", "a");
    f->AddLine("<mapper resource=\"com/dunchong/entity/mapper/" + mapper + ".xml\" />");
    f->Save();

    f->File::Open("com/dbmgr-spring-dubbo.xml", "a");
    f->AddLine("<dubbo:service interface=\"com.dunchong.service." + service + "\"");
	f->AddLine("	ref=\"" + obj + "Service\" timeout=\"5000\" />");
    f->Save();

    f->File::Open("com/wmgr-spring-dubbo.xml", "a");
    f->AddLine("<dubbo:reference id=\"" + obj + "Service\" interface=\"com.dunchong.service." + service + "\"");
	f->AddLine("	timeout=\"5000\" />");
    f->Save();

    cout << ToLowCase(entity) << ".title=" << endl;
    return 0;
}

class CBrowseDir
{
protected:
    //存放初始目录的绝对路径，以'\'结尾
    char m_soriginDir[_MAX_PATH];
    char m_szInitDir[_MAX_PATH];

public:
    //缺省构造器
    CBrowseDir();

    ~CBrowseDir();

    //设置初始目录为dir，如果返回false，表示目录不可用
    bool SetInitDir(const char *dir);

    //开始遍历初始目录及其子目录下由filespec指定类型的文件
    //filespec可以使用通配符 * ?，不能包含路径。
    //如果返回false，表示遍历过程被用户中止
    bool BeginBrowse(const char *filespec);

protected:
    //遍历目录dir下由filespec指定的文件
    //对于子目录,采用迭代的方法
    //如果返回false,表示中止遍历文件
    bool BrowseDir(const char *dir, const char *filespec);

    //函数BrowseDir每找到一个文件,就调用ProcessFile
    //并把文件名作为参数传递过去
    //如果返回false,表示中止遍历文件
    //用户可以覆写该函数,加入自己的处理代码
    virtual bool ProcessFile(const char *filename);

    //函数BrowseDir每进入一个目录,就调用ProcessDir
    //并把正在处理的目录名及上一级目录名作为参数传递过去
    //如果正在处理的是初始目录,则parentdir=NULL
    //用户可以覆写该函数,加入自己的处理代码
    //比如用户可以在这里统计子目录的个数
    virtual void ProcessDir(const char *currentdir, const char *parentdir);
};

CBrowseDir::CBrowseDir()
{
    //用当前目录初始化m_szInitDir
    getcwd(m_szInitDir, _MAX_PATH);

    getcwd(m_soriginDir, _MAX_PATH);

    //如果目录的最后一个字母不是'\',则在最后加上一个'\'
    int len = strlen(m_szInitDir);
    if (m_szInitDir[len - 1] != '\\')
        strcat(m_szInitDir, "\\");
}

CBrowseDir::~CBrowseDir()
{
    _chdir(m_soriginDir);
}

bool CBrowseDir::SetInitDir(const char *dir)
{
    //先把dir转换为绝对路径
    if (_fullpath(m_szInitDir, dir,_MAX_PATH) == NULL)
        return false;

    //判断目录是否存在
    if (_chdir(m_szInitDir) != 0)
        return false;

    //如果目录的最后一个字母不是'\',则在最后加上一个'\'
    int len = strlen(m_szInitDir);
    if (m_szInitDir[len - 1] != '\\')
        strcat(m_szInitDir, "\\");

    return true;
}

bool CBrowseDir::BeginBrowse(const char *filespec)
{
    ProcessDir(m_szInitDir, NULL);
    return BrowseDir(m_szInitDir, filespec);
}

bool CBrowseDir::BrowseDir(const char *dir, const char *filespec)
{
    _chdir(dir);

    //首先查找dir中符合要求的文件
    long hFile;
    _finddata_t fileinfo;
    if ((hFile=_findfirst(filespec, &fileinfo)) != -1)
    {
        do
        {
            //检查是不是目录
            //如果不是,则进行处理
            if (!(fileinfo.attrib & _A_SUBDIR))
            {
                char filename[_MAX_PATH];
                strcpy(filename, dir);
                strcat(filename, fileinfo.name);
                if (!ProcessFile(filename))
                    return false;
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    //查找dir中的子目录
    //因为在处理dir中的文件时，派生类的ProcessFile有可能改变了
    //当前目录，因此还要重新设置当前目录为dir。
    //执行过_findfirst后，可能系统记录下了相关信息，因此改变目录
    //对_findnext没有影响。
    _chdir(dir);
    if ((hFile = _findfirst("*.*", &fileinfo)) != -1)
    {
        do
        {
            //检查是不是目录
            //如果是,再检查是不是 . 或 ..
            //如果不是,进行迭代
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0
                    && strcmp(fileinfo.name, "..") != 0)
                {
                    char subdir[_MAX_PATH];
                    strcpy(subdir, dir);
                    strcat(subdir, fileinfo.name);
                    strcat(subdir, "\\");
                    ProcessDir(subdir, dir);
                    if (!BrowseDir(subdir, filespec))
                        return false;
                }
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return true;
}

bool CBrowseDir::ProcessFile(const char *filename)
{
    return true;
}

void CBrowseDir::ProcessDir(const char *currentdir, const char *parentdir)
{

}

//从CBrowseDir派生出的子类，用来统计目录中的文件及子目录个数
class CStatDir:public CBrowseDir
{
protected:
    int m_nFileCount;   //保存文件个数
    int m_nSubdirCount; //保存子目录个数
    KvVector fileList_;

public:
    //缺省构造器
    CStatDir()
    {
        //初始化数据成员m_nFileCount和m_nSubdirCount
        m_nFileCount = m_nSubdirCount = 0;
    }

    //返回文件个数
    int GetFileCount()
    {
        return m_nFileCount;
    }

    //返回子目录个数
    int GetSubdirCount()
    {
        //因为进入初始目录时，也会调用函数ProcessDir，
        //所以减1后才是真正的子目录个数。
        return m_nSubdirCount - 1;
    }

    KvVector GetFileList()
    {
        return fileList_;
    }

protected:
    //覆写虚函数ProcessFile，每调用一次，文件个数加1
    virtual bool ProcessFile(const char *filename)
    {
        m_nFileCount++;
        fileList_.Insert(filename, filename);
        return CBrowseDir::ProcessFile(filename);
    }

    //覆写虚函数ProcessDir，每调用一次，子目录个数加1
    virtual void ProcessDir(const char *currentdir, const char *parentdir)
    {
        m_nSubdirCount++;
        CBrowseDir::ProcessDir(currentdir, parentdir);
    }
};

int SearchPath(const string pathname, KvVector& fileList)
{
    //构造类对象
    CStatDir statdir;

    //设置要遍历的目录
    if (!statdir.SetInitDir(pathname.c_str()))
    {
        puts("目录不存在。");
        return -1;
    }

    //开始遍历
    statdir.BeginBrowse("*.*");

    fileList = statdir.GetFileList();
    return 0;
}

void printBlank(int k)
{
    for (int i = 0; i < k; i++)
    {
        cout << " ";
    }
}

#if 0
void parse(Value& v, int i)
{
    if (v.GetType() == json::ObjectVal)
    {
        printBlank(i);
        cout << "{" << endl;
        for (Object::ValueMap::iterator it = v.ToObject().begin(); it != v.ToObject().end(); it++)
        {
            printBlank(i);
            cout << it->first << ":";
            parse(it->second, i++);
        }
        printBlank(i);
        cout << "}" << endl;
    }
    else if (v.GetType() == json::ArrayVal)
    {
        printBlank(i);
        cout << "[" << endl;
        for (int j = 0; j < v.size(); j++)
        {
            parse(v[j], i++);
        }
        printBlank(i);
        cout << "]" << endl;
    }
    else if (v.GetType() == json::StringVal)
    {
        cout << v.ToString() << endl;
    }
    else if (v.GetType() == json::NULLVal)
    {
        cout << "null" << endl;
    }
}
#endif

int main(int argc, char** argv)
{
/*
    InitFunc(argv[0]);
    //Head();
    //GLOG(INFO) << "i'm a si message" << endl;
    //GLOG(ERROR) << "i'm a se message" << endl;
    cout << error_code << endl;

    cout << user_info.id << endl;
    CGILOG(INFO);
    ConfigFile kk("scrapbook.txt");
    CGILOG(INFO);
    File* ll = File::Open("aaa.txt", "w");
    CGILOG(INFO);
    KvMap ss = kk[0];
    CGILOG(INFO);
    for (int i = 0; i < ss.Size(); i++)
    {
        ll->AddLine(ss.GetKey(i));
    }
    ll->Save();
    CGILOG(INFO);
    return 0;
    */
    //cout << "Location:http://www.baidu.com" << "?wlanssid=34556" << endl << endl;

# if 1
    string configFile;
    if (argv[1] != NULL)
    {
        configFile = string(argv[1]);
        GeneralCode(configFile);
    }
    else
    {
        KvVector fileList;
        if (SearchPath("config", fileList) == 0)
        {
            for (int i = 0; i < fileList.Size(); i++)
                GeneralCode(fileList[i]);
        }
    }
#else
    File* ll = File::Open("ld2.xml", "r");

    string str = "";
    int iline = 0;
    int isingleFileLine = 0;
    File* psplitFile = File::Open(CIntStr(iline).ToString() + ".xml", "a");
    psplitFile->AddLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    psplitFile->AddLine("<?xml-stylesheet type=\"text/xsl\" href=\"cc.xsl\"?>");
    psplitFile->AddLine("<word>");
    while (ll->GetLine(str))
    {
        iline++;

        if (isingleFileLine < 3000)
        {
            psplitFile->AddLine(str);
            isingleFileLine++;
        }
        else
        {
            psplitFile->AddLine("</word>");
            psplitFile->Save();
            psplitFile = File::Open(CIntStr(iline).ToString() + ".xml", "a");
            psplitFile->AddLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            psplitFile->AddLine("<?xml-stylesheet type=\"text/xsl\" href=\"cc.xsl\"?>");
            psplitFile->AddLine("<word>");
            isingleFileLine = 0;
        }

    }
#endif

}

